package com.maori.languageapp;

import com.maori.languageapp.consts.CaptureFilterAdapter;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class LanguageAppSearchArea extends Activity {

	private static final String TAG = null;
	private CaptureFilterAdapter dbHelper;
	private SimpleCursorAdapter dataAdapter;
	ListView listViewitemslist;
	Button btnsearchclose;
	EditText searchfiltertext;
	String type, NCMID, node_category;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.language_app_search_area);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			type = extras.getString("type");
			NCMID = extras.getString("NCMID");
			node_category = extras.getString("node_category");
		}
		Log.v(TAG, "SEARCHTAG : " + type);

		listViewitemslist = (ListView) findViewById(R.id.listViewitemslist);
		btnsearchclose = (Button) findViewById(R.id.btnsearchclose);
		searchfiltertext = (EditText) findViewById(R.id.searchfiltertext);
		dbHelper = new CaptureFilterAdapter(this);
		dbHelper.open();

		btnsearchclose.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent i = new Intent(getApplicationContext(),
						LanguageAppCaptureActivity.class);
				i.putExtra("NCMID", NCMID);
				i.putExtra("node_category", node_category);
				i.putExtra("type", type);
				startActivity(i);
			}
		});
		// Word,Phrase,Story,Idiom,Song
		if (type.equals("Word")) {
			loadWordsDetailsList();
		} else if (type.equals("Phrase")) {
			loadPhraseDetailsList();
		} else if (type.equals("Story")) {
			loadStoryDetailsList();
		} else if (type.equals("Idiom")) {
			loadIdiomDetailsList();
		} else if (type.equals("Song")) {
			loadSongDetailsList();
		}
	}

	// *********Song List View Populating*************
	private void loadSongDetailsList() {
		// TODO Auto-generated method stub
		Cursor cursor = dbHelper.fetchAllSongItems();
		// The desired columns to be bound
		String[] columns = new String[] { CaptureFilterAdapter.KEY_song_title,
				CaptureFilterAdapter.KEY_song_content };
		String[] fromtrdes = new String[] { "coldes_1", "coldes_2" };
		// the XML defined views which the data will be bound to
		int[] to = new int[] { R.id.discover_maori_title,
				R.id.discover_english_title };
		// create the adapter using the cursor pointing to the desired data
		// as well as the layout information
		dataAdapter = new SimpleCursorAdapter(this,
				R.layout.language_capture_filter_list_raw, cursor, columns, to,
				0);

		ListView listView = (ListView) findViewById(R.id.listViewitemslist);
		// Assign adapter to ListView
		listView.setAdapter(dataAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> listView, View view,
					int position, long id) {
				// Get the cursor, positioned to the corresponding row in the
				// result set
				Cursor cursor = (Cursor) listView.getItemAtPosition(position);

				// Get the state's capital from this row in the database.
				String countryCode = cursor.getString(cursor
						.getColumnIndexOrThrow("word"));
				Toast.makeText(getApplicationContext(), countryCode,
						Toast.LENGTH_SHORT).show();

			}
		});

		searchfiltertext.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				dataAdapter.getFilter().filter(s.toString());
			}
		});

		dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
			public Cursor runQuery(CharSequence constraint) {
				return dbHelper.fetchPhraseItemsByName(constraint.toString());
			}
		});

	}

	// *********Idiom List View Populating*************
	private void loadIdiomDetailsList() {
		// TODO Auto-generated method stub
		Cursor cursor = dbHelper.fetchAllIdiomItems();
		// The desired columns to be bound
		String[] columns = new String[] { CaptureFilterAdapter.KEY_IdiomID,
				CaptureFilterAdapter.KEY_Idiom };
		String[] fromtrdes = new String[] { "coldes_1", "coldes_2" };
		// the XML defined views which the data will be bound to
		int[] to = new int[] { R.id.discover_maori_title,
				R.id.discover_english_title };
		// create the adapter using the cursor pointing to the desired data
		// as well as the layout information
		dataAdapter = new SimpleCursorAdapter(this,
				R.layout.language_capture_filter_list_raw, cursor, columns, to,
				0);

		ListView listView = (ListView) findViewById(R.id.listViewitemslist);
		// Assign adapter to ListView
		listView.setAdapter(dataAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> listView, View view,
					int position, long id) {
				// Get the cursor, positioned to the corresponding row in the
				// result set
				Cursor cursor = (Cursor) listView.getItemAtPosition(position);

				// Get the state's capital from this row in the database.
				String countryCode = cursor.getString(cursor
						.getColumnIndexOrThrow("word"));
				Toast.makeText(getApplicationContext(), countryCode,
						Toast.LENGTH_SHORT).show();

			}
		});

		searchfiltertext.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				dataAdapter.getFilter().filter(s.toString());
			}
		});

		dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
			public Cursor runQuery(CharSequence constraint) {
				return dbHelper.fetchPhraseItemsByName(constraint.toString());
			}
		});

	}

	// *********Story List View Populating*************
	private void loadStoryDetailsList() {
		// TODO Auto-generated method stub
		Cursor cursor = dbHelper.fetchAllStoryItems();
		// The desired columns to be bound
		String[] columns = new String[] { CaptureFilterAdapter.KEY_story_title,
				CaptureFilterAdapter.KEY_story_content };
		String[] fromtrdes = new String[] { "coldes_1", "coldes_2" };
		// the XML defined views which the data will be bound to
		int[] to = new int[] { R.id.discover_maori_title,
				R.id.discover_english_title };
		// create the adapter using the cursor pointing to the desired data
		// as well as the layout information
		dataAdapter = new SimpleCursorAdapter(this,
				R.layout.language_capture_filter_list_raw, cursor, columns, to,
				0);

		ListView listView = (ListView) findViewById(R.id.listViewitemslist);
		// Assign adapter to ListView
		listView.setAdapter(dataAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> listView, View view,
					int position, long id) {
				// Get the cursor, positioned to the corresponding row in the
				// result set
				Cursor cursor = (Cursor) listView.getItemAtPosition(position);

				// Get the state's capital from this row in the database.
				String countryCode = cursor.getString(cursor
						.getColumnIndexOrThrow("word"));
				Toast.makeText(getApplicationContext(), countryCode,
						Toast.LENGTH_SHORT).show();

			}
		});

		searchfiltertext.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				dataAdapter.getFilter().filter(s.toString());
			}
		});

		dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
			public Cursor runQuery(CharSequence constraint) {
				return dbHelper.fetchPhraseItemsByName(constraint.toString());
			}
		});
	}

	// *********Phrase List View Populating*************
	private void loadPhraseDetailsList() {
		// TODO Auto-generated method stub
		Cursor cursor = dbHelper.fetchAllPhraseItems();
		// The desired columns to be bound
		String[] columns = new String[] { CaptureFilterAdapter.KEY_PhraseID,
				CaptureFilterAdapter.KEY_Phrase };
		String[] fromtrdes = new String[] { "coldes_1", "coldes_2" };
		// the XML defined views which the data will be bound to
		int[] to = new int[] { R.id.discover_maori_title,
				R.id.discover_english_title };
		// create the adapter using the cursor pointing to the desired data
		// as well as the layout information
		dataAdapter = new SimpleCursorAdapter(this,
				R.layout.language_capture_filter_list_raw, cursor, columns, to,
				0);

		ListView listView = (ListView) findViewById(R.id.listViewitemslist);
		// Assign adapter to ListView
		listView.setAdapter(dataAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> listView, View view,
					int position, long id) {
				// Get the cursor, positioned to the corresponding row in the
				// result set
				Cursor cursor = (Cursor) listView.getItemAtPosition(position);

				// Get the state's capital from this row in the database.
				String countryCode = cursor.getString(cursor
						.getColumnIndexOrThrow("word"));
				Toast.makeText(getApplicationContext(), countryCode,
						Toast.LENGTH_SHORT).show();

			}
		});

		searchfiltertext.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				dataAdapter.getFilter().filter(s.toString());
			}
		});

		dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
			public Cursor runQuery(CharSequence constraint) {
				return dbHelper.fetchPhraseItemsByName(constraint.toString());
			}
		});
	}

	// *********Word List View Populating*************
	private void loadWordsDetailsList() {
		// TODO Auto-generated method stub
		Cursor cursor = dbHelper.fetchAllItems();
		// The desired columns to be bound
		String[] columns = new String[] { CaptureFilterAdapter.KEY_WordID,
				CaptureFilterAdapter.KEY_word };
		String[] fromtrdes = new String[] { "coldes_1", "coldes_2" };
		// the XML defined views which the data will be bound to
		int[] to = new int[] { R.id.discover_maori_title,
				R.id.discover_english_title };
		// create the adapter using the cursor pointing to the desired data
		// as well as the layout information
		dataAdapter = new SimpleCursorAdapter(this,
				R.layout.language_capture_filter_list_raw, cursor, columns, to,
				0);

		ListView listView = (ListView) findViewById(R.id.listViewitemslist);
		// Assign adapter to ListView
		listView.setAdapter(dataAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> listView, View view,
					int position, long id) {
				// Get the cursor, positioned to the corresponding row in the
				// result set
				Cursor cursor = (Cursor) listView.getItemAtPosition(position);

				// Get the state's capital from this row in the database.
				String countryCode = cursor.getString(cursor
						.getColumnIndexOrThrow("word"));
				Toast.makeText(getApplicationContext(), countryCode,
						Toast.LENGTH_SHORT).show();

			}
		});

		searchfiltertext.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				dataAdapter.getFilter().filter(s.toString());
			}
		});

		dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
			public Cursor runQuery(CharSequence constraint) {
				return dbHelper.fetchItemsByName(constraint.toString());
			}
		});
	}
}
